package beautiful.butterfly.drds.binlog.zookeeper;

import beautiful.butterfly.drds.binlog.utils.Constants;
import beautiful.butterfly.drds.binlog.utils.PropertiesExtend;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class CuratorFrameworkClient
{

    private static CuratorFrameworkClient curatorFrameworkClient;
    private CuratorFramework curatorFramework;

    private CuratorFrameworkClient(CuratorFramework curatorFramework)
    {
        this.curatorFramework = curatorFramework;
    }

    public static CuratorFrameworkClient getCuratorFrameworkClient()
    {
        if (null == curatorFrameworkClient)
        {
            synchronized (CuratorFrameworkClient.class)
            {
                if (null == curatorFrameworkClient)
                {
                    CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                            .connectString(PropertiesExtend.getInstance().getZk_servers()).sessionTimeoutMs(5000)
                            .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
                    curatorFramework.start();
                    curatorFramework.usingNamespace(Constants.ZK_NAMESPACE);
                    curatorFrameworkClient = new CuratorFrameworkClient(curatorFramework);
                }
            }
        }
        return curatorFrameworkClient;
    }

    public CuratorFramework getCuratorFramework()
    {
        return curatorFramework;
    }
}
